package com.bdqn.wang.servlet;

import com.alibaba.fastjson.JSONArray;
import com.bdqn.wang.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/jqgrid"})
public class JqServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String opr = req.getParameter("opr");
        if("to".equals(opr)){
            req.getRequestDispatcher("/WEB-INF/table_jqgrid.jsp").forward(req,resp);
        }else if("list".equals(opr)){
            List<User> list = new ArrayList<User>();
            for (int i = 0;i<100;i++){
                User u = new User(i,"小明"+i);
                list.add(u);
            }
            String json = JSONArray.toJSONString(list);
            resp.getWriter().print(json);
        }
    }
}
